﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlTypes;
using Logic;
using Helper;
using DBAccess;
using WHUT;

public partial class tokenTeam_messageAdm : System.Web.UI.Page
{
    IPagedDataProvider pdp = PagedDataProviderFactory.Instance();
    MessageBoard msgBoard = new MessageBoard();

    protected void Page_Load(object sender, EventArgs e)
    {
        pdp.ConnectionName = Common.GetDefaultSqlConnectionName();
        pdp.TableOrView = "t_messageboard";
        pdp.FieldsToReturn = "id,title, nickname, time_add, email, content";
        //pdp.FieldsToReturn = "*";
        pdp.FieldToOrder = "id";
        pdp.Ascending = false;
        pdp.PageSize = spPager.PageSize;
        pdp.RecordsFilter = "1=1";
         
        if (!IsPostBack)
        {
            BindMessage(true);
        }
    }

    private void BindMessage(bool bResetPager)
    {
        if (bResetPager)
        {
            spPager.CurrentPageIndex = 1;
            spPager.RecordsCount = pdp.TotalRecords;
        }

        pdp.CurrentPageIndex = spPager.CurrentPageIndex;
        rptMsgBox.DataSource = pdp.GetCurrentPageAsDataTable().DefaultView;
        rptMsgBox.DataBind();
    }

    /// <summary>
    /// 翻页控制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SimplePager1_PageIndexChanging(object sender, wbh.PageChangingEvent e)
    {
        spPager.CurrentPageIndex = e.NewIndex;
        BindMessage(false);
    }


    protected void repMsgBox_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        string selStr = string.Empty;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rep = e.Item.FindControl("rpt_MsgRelpy") as Repeater;//找到里层的repeater对象
            DataRowView rowv = (DataRowView)e.Item.DataItem; //找到分类Repeater关联的数据项 
            String deptId = Convert.ToString(rowv["Id"]);


            selStr = "SELECT [id], [content],[reply_time] FROM [t_message_reply] WHERE [msg_id]=" + deptId;
            DataTable dt = Common.SQLQueryAsDataTable(Common.GetDefaultDBConnection(), selStr);
            rep.DataSource = dt;
            rep.DataBind();
        }
    }

    protected void msgDelett_Click(object sender, EventArgs e)
    {
        //int id = Convert.ToInt32(e.CommandName);
        int id = Function.IntSafeConvert(((ImageButton)sender).CommandArgument);
        string msg = MessageBoard.DeleteById(id);
        if(msg != null)
        {
            Response.Redirect("~/error.htm");
            return;
        }
        BindMessage(true);
    }


    protected void replyDelet_Click(object sender, EventArgs e)
    {
        int replyId = Function.IntSafeConvert(((ImageButton)sender).CommandArgument);
        try
        {
            Common.SQLUpdate(Common.GetDefaultDBConnection(), "DELETE FROM  [dbo].[t_message_reply] WHERE [id] = @1 ;", true, new object[] { replyId });
        }
        catch (Exception ex)
        {
            Response.Redirect("~/error.htm");
            return;
        }
        BindMessage(true);
    }

    protected void imgbtDelAll_Click(object sender, EventArgs e)
    {
        string flag = string.Empty;
        int[] idArr = new int[rptMsgBox.Items.Count + 1]; 
        
        for (int i = 0; i < rptMsgBox.Items.Count; i++)
        {
            Label lb = (Label)rptMsgBox.Items[i].FindControl("lbMsgId");
            idArr[i] = Function.IntSafeConvert(lb.Text);

        }
        flag = MessageBoard.DeleteByArray(idArr);
        if(flag != null)
        {
            Response.Redirect("~/error.htm");
            return;
        }
        BindMessage(true);
    }



    protected void imgbtDelSelected_Click(object sender, EventArgs e)
    {
        string flag = string.Empty;
        int[] idArr = new int[rptMsgBox.Items.Count + 1];

        for (int i = 0; i < rptMsgBox.Items.Count; i++)
        {
            Label lb = (Label)rptMsgBox.Items[i].FindControl("lbMsgId");
            CheckBox chb = (CheckBox)rptMsgBox.Items[i].FindControl("ckbDelete");
            if (!chb.Checked) { continue; }
            idArr[i] = Function.IntSafeConvert(lb.Text);

        }
        flag = MessageBoard.DeleteByArray(idArr);
        if (flag != null)
        {
            Response.Redirect("~/error.htm");
            return;
        }
        BindMessage(true);
    }

    protected void ibSubmit_Click(object sender, EventArgs e)
    {
        string replyContent = tbContent.Text.Trim();
        int msgId = Function.IntSafeConvert(lbMsgId.Text);


        try
        {
            //Common.SQLUpdate(Common.GetDefaultDBConnection(), "INSERT INTO [dbo].[t_message] ([title],[content],[file_url],[sender],[receiver],[is_read],[sender_delete],[receiver_delete],[time_post])  VALUES ( @1,@2,@3,@4,@5,@6,@7,@8,@9 );", true, new object[] { _title, _content, _file_url, _sender, _receiver, _is_read, _sender_delete, _receiver_delete, _time_post });
            Common.SQLUpdate(Common.GetDefaultDBConnection(), "INSERT INTO [dbo].[t_message_reply] ([msg_id],[content],[reply_time])  VALUES ( @1,@2,@3) ;", true, new object[] { msgId, replyContent, (SqlDateTime)DateTime.Now });
        }
        catch (Exception ex)
        {
            Response.Redirect("~/error.htm");
            return;
        }

        BindMessage(true);
    }


    protected void msgReply_Click(object sender, EventArgs e)
    {
        int id = Function.IntSafeConvert(((ImageButton)sender).CommandArgument);
        msgBoard = MessageBoard.LoadById(id);

        if (null == msgBoard)
        {
            Response.Redirect("~/error.htm");
            return;
        }

        lbMsgTitle.Text = msgBoard.Title;
        lbMsgId.Text = msgBoard.Id.ToString();
        lbMsgTime.Text = Helper.Function.DateTimeSafeConvert(msgBoard.TimeAdd).ToString("yyyy-M-d mm:ss");
        lbMsgContent.Text = msgBoard.Content;
        tbContent.Focus();
    }
}
